package cn.yanjr.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import cn.yanjr.common.bean.BeanFactory;
import cn.yanjr.common.utils.Result;
import cn.yanjr.system.entity.SysUser;
import cn.yanjr.system.service.SysUserService;

/**
 * 获取当前用户相关信息工具类
 * @author boc
 */
public class CurrentInfoUtil {

    private static Logger log = LoggerFactory.getLogger(CurrentInfoUtil.class);

    private static CurrentInfoUtil u = null;

    private SysUserService sysUserService = BeanFactory.getBean("sysUserService", SysUserService.class);

    /**
     * @return 当前用户对象
     */
    public static SysUser getUser(HttpServletRequest request) {
//        return getUserById(getReq().getParameter("loginId"));
    	return getUserById(request.getHeader("loginId"));
    }

    /**
     * @return 当前用户id
     */
    public static String getUserId(HttpServletRequest request) {
//        return getReq().getParameter("loginId");
        return request.getHeader("loginId");
    }

    /**
     * @param id 用户id
     * @return 指定id的用户对象
     */
    public static SysUser getUserById(String id) {
        return u.sysUserService.get(id);
    }

    /**
     * @param name 用户登录名
     * @return 指定id的用户对象
     */
    public static SysUser getUserByName(String name) {
        return u.sysUserService.getUserByLoginName(name);
    }

    /**
     * 从请求中获取参数
     * @param name 参数名
     * @return 参数值
     */
    public static String getParam(HttpServletRequest request, String name) {
        String[] values = request.getParameterValues(name);
        if (values == null || values.length == 0) {
            return null;
        }
        if (values.length > 1) {
            log.error("请注意!" + name + "参数有多个值");
        }
        return values[0];
    }
    
    /**
     * 获取request参数封装到map中
     * @param request
     * @return
     */
    public static Map<String, Object> getParamMap(HttpServletRequest request) {
    	Map<String, Object> params = new HashMap<>(4, 1F);
    	request.getParameterMap().forEach((k, v) -> params.put(k, v[0]));
    	return params;
    }
    
    /**
     * 信息输出到前端
     * @param response
     * @param result
     * @throws IOException
     */
    public static void responsePrint(HttpServletResponse response, Result result) throws IOException {
    	PrintWriter out = response.getWriter();
    	out.println(JSON.toJSONString(result));
    }

	/**
	 * 获取IP地址
	 * @param request
	 * @return String
	 */
	public static String getRemoteHost(javax.servlet.http.HttpServletRequest request){
	    String ip = request.getHeader("x-forwarded-for");
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
	        ip = request.getHeader("Proxy-Client-IP");
	    }
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
	        ip = request.getHeader("WL-Proxy-Client-IP");
	    }
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
	        ip = request.getRemoteAddr();
	    }
	    return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
	}

}
